package org.urbansoft.icl.record;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.urbansoft.icl.field.Field;
import org.urbansoft.icl.field.GenericANField;
import org.urbansoft.icl.field.GenericANSField;
import org.urbansoft.icl.field.GenericBlankField;
import org.urbansoft.icl.field.GenericNField;
import org.urbansoft.icl.field.Position;
import org.urbansoft.icl.field.Usage;
import org.urbansoft.icl.field.ValidationCriteria;
import org.urbansoft.icl.field.validation.RecordTypeValidator;

/**
 * The Class BundleControlRecord.
 */
public class BundleControlRecord extends Record {
	private static Logger log = Logger.getLogger(BundleControlRecord.class);
	public Field recordType;
	public Field itemCount;
	public Field bundleAmount;
	public Field micrAmount;
	public Field imagesInBundleCount;
	public Field userField;
	public Field reserved;

	/**
	 * Instantiates a new bundle control record.
	 */
	public BundleControlRecord() {
		fields = new ArrayList<Field>();
		recordName = "Bundle Control Record";
		recordTypeNumber = "70";
		initFields();
	}

	public BundleControlRecord(String read) {
		// log.info("New BundleControlRecord for READ");
		fields = new ArrayList<Field>();
		recordName = "Bundle Control Record";
		recordTypeNumber = "70";
		initFields2();
	}

	public BundleControlRecord(int read) {
		// log.info("New BundleControlRecord for WRITE");
		fields = new ArrayList<Field>();
		recordName = "Bundle Control Record";
		recordTypeNumber = "70";
		initFields3();
	}

	/**
	 * Inits the fields for read.
	 */
	private void initFields2() {
		try {
			/* check detail record */

			recordType = new GenericANField(new Position(1, 2), "Record Type");
			recordType.setValidationCriteria(ValidationCriteria.REQUIRED);

			/* item count */
			itemCount = new GenericANField(new Position(3, 6),
					"Items Within Bundle Count");

			/* bundle amount */
			bundleAmount = new GenericANField(new Position(7, 23),
					"Bundle Total Amount");

			/* micr amount */
			micrAmount = new GenericANField(new Position(24, 40),
					"MICR Valid Total Amount");
			micrAmount.setUsage(Usage.CONDITIONAL);

			/* images within bundle count */
			imagesInBundleCount = new GenericANField(new Position(41, 45),
					"Images Within Bundle Count");
			imagesInBundleCount.setUsage(Usage.CONDITIONAL);

			/* user field - NOT USED */
			userField = new GenericANField(new Position(46, 65), "User Field");
			userField.setUsage(Usage.CONDITIONAL);

			/* Reserved - NOT USED */
			reserved = new GenericANField(new Position(66, 90),
					"Reserved Field");
			// reserved.setFieldData("");

			fields.add(recordType);
			fields.add(itemCount);
			fields.add(bundleAmount);
			fields.add(micrAmount);
			fields.add(imagesInBundleCount);
			fields.add(userField);
			fields.add(reserved);

		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}

	/**
	 * Inits the fields for read.
	 */
	private void initFields3() {
		try {
			/* check detail record */

			recordType = new GenericANField(new Position(1, 2),
					RecordTypeValidator.INSTANCE, "70", "Record Type");

			/* item count */
			itemCount = new GenericANField(new Position(3, 6),
					"Items Within Bundle Count");
			itemCount.setFieldData("");

			/* bundle amount */
			bundleAmount = new GenericANField(new Position(7, 23),
					"Bundle Total Amount");
			bundleAmount.setFieldData("");

			/* micr amount */
			micrAmount = new GenericANField(new Position(24, 40),
					"MICR Valid Total Amount");
			micrAmount.setFieldData("");

			/* images within bundle count */
			imagesInBundleCount = new GenericANField(new Position(41, 45),
					"Images Within Bundle Count");
			imagesInBundleCount.setFieldData("");

			/* user field - NOT USED */
			userField = new GenericANField(new Position(46, 65), "User Field");
			userField.setFieldData("");

			/* Reserved - NOT USED */
			reserved = new GenericANField(new Position(66, 90),
					"Reserved Field");
			reserved.setFieldData("");

			fields.add(recordType);
			fields.add(itemCount);
			fields.add(bundleAmount);
			fields.add(micrAmount);
			fields.add(imagesInBundleCount);
			fields.add(userField);
			fields.add(reserved);

		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}

	/**
	 * Inits the fields.
	 */
	private void initFields() {
		try {
			/* check detail record */
			recordType = new GenericNField(new Position(1, 2),
					RecordTypeValidator.INSTANCE, "70", "Record Type");
			recordType.setValidationCriteria(ValidationCriteria.REQUIRED);

			/* item count */
			itemCount = new GenericNField(new Position(3, 6),
					"Items Within Bundle Count");

			/* bundle amount */
			bundleAmount = new GenericNField(new Position(7, 18),
					"Bundle Total Amount");

			/* micr amount */
			micrAmount = new GenericNField(new Position(19, 30),
					"MICR Valid Total Amount");
			micrAmount.setUsage(Usage.CONDITIONAL);

			/* images within bundle count */
			imagesInBundleCount = new GenericNField(new Position(31, 35),
					"Images Within Bundle Count");
			imagesInBundleCount.setUsage(Usage.CONDITIONAL);

			/* user field - NOT USED */
			userField = new GenericANSField(new Position(36, 55), "User Field");
			userField.setUsage(Usage.CONDITIONAL);

			/* Reserved - NOT USED */
			reserved = new GenericBlankField(new Position(56, 80),
					"Reserved Field");
			reserved.setFieldData("");

			fields.add(recordType);
			fields.add(itemCount);
			fields.add(bundleAmount);
			fields.add(micrAmount);
			fields.add(imagesInBundleCount);
			fields.add(userField);
			fields.add(reserved);

		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}

}
